local skynet = require "skynet"
local filelog = require "filelog"
local base = require "base"
local timer = require "timer"
local timetool = require "timetool"
local filename = "trainlogic.lua"

require "pubenum"
require "sssenum"

local train_mirrors = {
    mirror_lists = {
       --[[
            [m_rid] = {
                state = 0,  0 空闲, 1 在桌子中 2 缺钱被踢出桌子
                train_score = 0,
                diamond = 0,
                rolename = "陪练"
            }
       --]]
    },
    max_mirrors = DNPlayerNum.DN_TABLE_PLAYER_NUM_SEVEN,     ---最大陪练数量
    begin_rid = 0,
}


local trainlogic = {
    server = nil,
}

function trainlogic.init(service)
    if trainlogic.server == nil then
        trainlogic.server = service
    end
end


function trainlogic.init_mirrors(table_index)
    local server = trainlogic.server
    local table_data = server.table_data
    train_mirrors.max_mirrors = table_data.conf.max_player_num
    if train_mirrors.begin_rid == 0 then train_mirrors.begin_rid = table_index * 100 + 0 end
    for i = 1, train_mirrors.max_mirrors do
        train_mirrors.begin_rid = train_mirrors.begin_rid + 1
        train_mirrors.mirror_lists[train_mirrors.begin_rid] = {
            rid = train_mirrors.begin_rid,
            state = 0,
            train_score = 100000,
            diamond = 1000000,
            rolename = "陪练" .. tostring(train_mirrors.begin_rid % 10),
            leave_times = 0,
        }
    end
end

function trainlogic.resetmirror()
    for mirror_rid, mirror in pairs(train_mirrors.mirror_lists) do
        if mirror.state == 2 then
            mirror.train_score = 100000
            mirror.diamond = 1000000
        end
    end
end

function trainlogic.get_idle_mirror()
    local idle_rid = 0
    for index, mirror_obj in pairs(train_mirrors.mirror_lists) do
        if mirror_obj.state == 0 then
            idle_rid = mirror_obj.rid
            break
        end
    end
    return idle_rid
end

function trainlogic.get_mirror_num()
    local server = trainlogic.server
    local table_data = server.table_data
    local mirror_num = 0
    for index, seat in ipairs(table_data.seats) do
        if seat.rid ~= 0 and seat.is_mirror == true then
            mirror_num = mirror_num + 1
        end
    end
    return mirror_num
end

function trainlogic.entersittable(mirror_num)
    local server = trainlogic.server
    local table_data = server.table_data
    local tablelogic = server:get_logicbyname("tablelogic")
    if mirror_num > table_data.conf.max_player_num then mirror_num = table_data.conf.max_player_num end
    for i = 1, mirror_num do
        local idle_rid = trainlogic.get_idle_mirror()
        local mirror_num = trainlogic.get_mirror_num()
        local idle_mirror = train_mirrors.mirror_lists[idle_rid]
        if idle_rid ~= 0 and idle_mirror ~= nil and idle_mirror.state == 0 and mirror_num <= table_data.conf.max_player_num then
            local get_seat = tablelogic.get_emptyseat_by_index(table_data, 0)
            if get_seat ~= nil then
                table_data.sitdown_player_num = table_data.sitdown_player_num + 1
		        table_data.conf.cur_player_num = table_data.conf.cur_player_num + 1
		        server:updateplayernum()
                get_seat.state = SSSSeatState.SEAT_STATE_WAIT_READY
                get_seat.rid = idle_mirror.rid
                get_seat.train_score = idle_mirror.train_score
                get_seat.diamond = idle_mirror.diamond
                get_seat.playerinfo.rolename = idle_mirror.rolename
                get_seat.gatesvr_id = "node"
	            get_seat.agent_address = "-1"
	            get_seat.playerinfo.logo = "2"
	            get_seat.playerinfo.sex	= 1 
	            get_seat.playerinfo.diamond = 0
	            get_seat.playerinfo.fangka = 10
	            get_seat.playerinfo.ip_address = ""
	            get_seat.playerinfo.intro = ""
                get_seat.playerinfo.upvoted_num = 0
                get_seat.playerinfo.downvoted_num = 0
                get_seat.playerinfo.gps_addr = ""
                get_seat.playerinfo.logoframe = 0
                get_seat.ready_to_time = timetool.get_time() + table_data.conf.ready_timeout
	            get_seat.is_disconnected = 0
                get_seat.is_mirror = true
                if table_data.state ~= SSSTableState.DN_TABLE_STATE_WAIT_PLAYER_SITDOWN and 
		            table_data.state ~= SSSTableState.DN_TABLE_STATE_WAIT_ALL_READY then
		            get_seat.state = SSSSeatState.SEAT_STATE_WAIT_NEXT_GAME
	            end
                local noticemsg = {
		            rid = get_seat.rid,
		            seat_info = {},
		            seat_playerinfo = {},
	            }
	            tablelogic.copy_seat_baseinfo(noticemsg.seat_info, get_seat)
	            tablelogic.copy_seat_playerinfo(noticemsg.seat_playerinfo, get_seat)
	            local sendrids = tablelogic.getallrids(table_data)
                if tablelogic.has_real_player() then
	                server:send_notice_to_players(sendrids, "sitdowntableN", noticemsg, nil, nil)
                end
                if get_seat.ready_timer_id > 0 then
		            timer.cleartimer(get_seat.ready_timer_id)
		            get_seat.ready_timer_id = -1
	            end
	            if get_seat.state == SSSSeatState.SEAT_STATE_WAIT_READY then
		            local outoftimermsg = {
			            table_index = table_data.conf.table_index,
			            rid = get_seat.rid,
			            seat_index = get_seat.index,
		            }
		            get_seat.ready_timer_id = timer.settimer(base.get_random(2,3)*100, "mirror_doready", outoftimermsg)
    	            get_seat.ready_to_time = timetool.get_time() + table_data.conf.ready_timeout
		            local doreadyntcmsg = {
			            rid = get_seat.rid,
			            seat_index = get_seat.index,
			            ready_to_time = timetool.get_time() + table_data.conf.ready_timeout,
		            }
                    if tablelogic.has_real_player() then
		                server:send_notice_to_players(sendrids, "doreadyN", doreadyntcmsg, nil, nil)
                    end
	            end
                if table_data.state == SSSTableState.DN_TABLE_STATE_WAIT_PLAYER_SITDOWN then
                    table_data.state = SSSTableState.DN_TABLE_STATE_WAIT_ALL_READY
                end
                idle_mirror.state = 1
            end
        end
    end
end

function trainlogic.do_ready(mirror_rid)
    local server = trainlogic.server
    local table_data = server.table_data
    local tablelogic = server:get_logicbyname("tablelogic")
    local mirror = train_mirrors.mirror_lists[mirror_rid]
    if mirror then
        if mirror.state == 1 then
            local seat = tablelogic.get_seat_by_rid(table_data, mirror.rid)
            if seat == nil then
                return
            end
            if table_data.state ~= SSSTableState.DN_TABLE_STATE_WAIT_ALL_READY then
		        return
	        end
            seat.state = SSSSeatState.SEAT_STATE_WAIT_START
	        local noticemsg = {
		        rid = seat.rid,
		        seat_index = seat.index,
		        is_ready = 0,
		        table_state = 0,
	        }
	        if seat.state == SSSSeatState.SEAT_STATE_WAIT_START then noticemsg.is_ready = 1 end
	        if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then
		        seat.train_score = mirror.train_score
            elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZUANSI then
                seat.diamond = mirror.diamond
	        end
            if not table_data.conf.is_train_diamond or table_data.conf.is_train_diamond == 0 then
                if not tablelogic.has_real_player() then
			        for index, seat in ipairs(table_data.seats) do
				        if seat.rid ~= 0 and seat.is_mirror == true and seat.state == SSSSeatState.SEAT_STATE_WAIT_START then
					       seat.state = SSSSeatState.SEAT_STATE_WAIT_READY
					       seat.ready_to_time = 0
					       if seat.ready_timer_id > 0 then
						      timer.cleartimer(seat.ready_timer_id)
						      seat.ready_to_time = -1
					       end
				        end
			         end
                end
		    end

	        local is_allready = tablelogic.check_all_ready(table_data)
	        local readynum = 0
	        for k, v in ipairs(table_data.seats) do
		        if v.rid ~= 0 and v.state == SSSSeatState.SEAT_STATE_WAIT_START then
			        readynum = readynum + 1
		        end
	        end
	        if readynum > 1  and is_allready == true and table_data.state == SSSTableState.DN_TABLE_STATE_WAIT_ALL_READY then
		        table_data.is_can_kick = false
		        table_data.state = SSSTableState.DN_TABLE_STATE_ONE_GAME_START
		        local gamelogic = tablelogic.get_gamelogic()
		        gamelogic.run(table_data.gamelogic)
            elseif readynum <= 1 and is_allready == true then
		        if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then
			        if table_data.timer_id > 0 then
				        timer.cleartimer(table_data.timer_id)
				        table_data.timer_id =-1
			        end
			        local timernoticemsg = {
        			    table_index = table_data.conf.table_index,
        			    neednum = 1,
    			    }
    		        table_data.timer_id = timer.settimer(1*100, "train_add_mirror", timernoticemsg)
                end
		    end
	        noticemsg.table_state = table_data.state
	        local sendrids = tablelogic.getallrids(table_data)
            if tablelogic.has_real_player() then
	            server:send_notice_to_players(sendrids, "gamereadyN", noticemsg, nil, nil)
            end
		end
    end
end

function trainlogic.balance_score()
    local server = trainlogic.server
    local table_data = server.table_data
    if table_data.conf.table_type == SSSTableType.TABLE_TYPE_TRAIN then
        for index, seat in ipairs(table_data.seats) do
            if seat.rid ~= 0 and seat.is_mirror == true then
                local mirror = train_mirrors.mirror_lists[seat.rid]
                if mirror then
                    if mirror.train_score ~= seat.train_score then mirror.train_score = seat.train_score end
                    if mirror.diamond ~= seat.diamond then mirror.diamond = seat.diamond end
                end
            end
        end
    elseif table_data.conf.table_type == SSSTableType.TABLE_TYPE_ZUANSI then
        if table_data.conf.is_train_diamond == 1 then
            for index, seat in ipairs(table_data.seats) do
                if seat.rid ~= 0 and seat.is_mirror == true then
                    local mirror = train_mirrors.mirror_lists[seat.rid]
                    if mirror then
                        if mirror.train_score ~= seat.train_score then mirror.train_score = seat.train_score end
                        if mirror.diamond ~= seat.diamond then mirror.diamond = seat.diamond end
                    end
                end
            end
        end
    end
end

function trainlogic.leavetable(rid)
    local mirror = train_mirrors.mirror_lists[rid]
    if mirror then
        mirror.state = 2
        mirror.train_score = 100000
        mirror.diamond = 1000000
        mirror.state = 0
        mirror.leave_times = mirror.leave_times + 1
    end
end

function trainlogic.check_all_mirror_readytime()
    local server = trainlogic.server
    local table_data = server.table_data
    local tablelogic = server:get_logicbyname("tablelogic")
    if tablelogic.has_real_player() then
        for index, seat in ipairs(table_data.seats) do
            if seat.is_mirror == true then
                seat.ready_to_time = timetool.get_time() + table_data.conf.ready_timeout
                seat.action_to_time = timetool.get_time() + table_data.conf.ready_timeout
                if seat.ready_timer_id > 0 then
                    timer.cleartimer(seat.ready_timer_id)
                    seat.ready_timer_id = -1
                end
                local outoftimermsg = {
			        table_index = table_data.conf.table_index,
			        rid = seat.rid,
			        seat_index = seat.index,
		        }
                seat.ready_timer_id = timer.settimer(base.get_random(2,3)*100, "mirror_doready", outoftimermsg)
            end
        end
    end
end

return trainlogic